package com.example.demo;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yishuangshuang
 * @ClassName: StreamDemo5
 * @Description: TODO
 * @date 2019/1/5 12:59 PM
 */
public class StreamDemo5 {
    public static void main(String[] args) {
        Stream.iterate(0, n -> n + 3).limit(10). forEach(x -> System.out.print(x + " "));

        System.out.println("=========");
        test();

        System.out.println("____________");
        test1();
    }

    public static void test(){
        Map<Integer, List<Person>> personGroups = Stream.generate(new StreamDemo4.PersonSupplier()).
                limit(100).
                collect(Collectors.groupingBy(Person::getAge));
        Iterator it = personGroups.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Integer, List<Person>> persons = (Map.Entry) it.next();
            System.out.println("Age " + persons.getKey() + " = " + persons.getValue().size());
        }
    }

    public static void test1(){
        Map<Boolean, List<Person>> children = Stream.generate(new StreamDemo4.PersonSupplier()).
                limit(100).
                collect(Collectors.partitioningBy(p -> p.getAge() < 18));
        System.out.println("Children number: " + children.get(true).size());
        System.out.println("Adult number: " + children.get(false).size());

    }
}
